package com.flying.carclub.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.flying.carclub.BaseActivity;
import com.flying.carclub.R;
import com.flying.carclub.api.ApiImpl;
import com.flying.carclub.http.OnResponseListener;
import com.flying.carclub.utils.StringUtil;
import com.flying.carclub.widget.TitleBar;

public class FeedbackActivity extends BaseActivity {
    private TitleBar titleBar;
    private EditText contentEt;
    private EditText mobileEt;
    private TextView numTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initView();
    }
    private void initView(){
        titleBar = findViewById(R.id.bar_title);
        contentEt = findViewById(R.id.content);
        numTv = findViewById(R.id.str_num);
        mobileEt = findViewById(R.id.user_mobile);
        contentEt.addTextChangedListener(textWatcher);
        titleBar.setOnTitleClickListener(new TitleBar.OnTitleClickListener() {
            @Override
            public boolean onLeftClick() {
                return false;
            }

            @Override
            public void onRightClick() {
                if (contentEt.getText().toString().trim().isEmpty()) {
                    showToast("先输入才能提交喔");
                    return;
                }
                if (TextUtils.isEmpty(mobileEt.getText())) {
                    showToast("请输入手机");
                    return;
                }

                if (!StringUtil.isMobileNumber(mobileEt.getText().toString())) {
                    showToast("请输入11位正确的手机号码");
                    return;
                }
                submit();
            }
        });
    }

    TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() <= 140) {
                numTv.setText(String.valueOf(140 - s.toString().length()));
            } else {
                contentEt.setText(s.toString().substring(0, s.toString().length() > 140 ? 140 : s.toString().length()));
                contentEt.setSelection(s.toString().length() > 140 ? 140 : s.toString().length());
            }
        }
    };

    private void submit() {
        showLoadingDialog();
        new ApiImpl().httpFeedback(contentEt.getText().toString(), mobileEt.getText().toString(), new OnResponseListener() {
            @Override
            public void onSuccess(Object response) {
                dismissLoadingDialog();
                showToast("您的反馈已经提交 ，我们会尽快给您回复");
                FeedbackActivity.this.finish();
            }

            @Override
            public void onError(String err) {
                dismissLoadingDialog();
                showToast("网络请求失败");
            }
        });
    }
}
